package com.springboot.cloud.order.service;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.springboot.cloud.common.web.entity.vo.SecKillMessage;
import com.springboot.cloud.order.entity.Order;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;


/**
 * @author godyao
 * @date 2022/3/18
 */
@Slf4j
@Component
public class RabbitMqConsumer {
    @Autowired
    private IOrderService orderService;

    @RabbitListener(queues = "secKillQueue")
    public void consume(String message) {
        log.info("收到消息：msg={}", message);
        try {
            final SecKillMessage secKillMessage = new ObjectMapper().readValue(message, SecKillMessage.class);
            final Order order = orderService.makeOrder(secKillMessage);
            log.info("下单成功,订单号为{}", order.getId());
        } catch (Exception e) {
            log.error("下单失败, msg={}", e.getLocalizedMessage());
        }
    }
}
